package models

import "time"

type ArticleCategories struct {
	ID        int       `gorm:"primary_key" json:"id"`
	Title     string    `json:"title"`
	Sort      int       `json:"sort"`
	ParentId  int       `json:"parent_id"`
	IsEnabled int       `json:"is_enabled"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
}

type ArticleCategoriesResult struct {
	ID        int       `gorm:"primary_key" json:"id"`
	Title     string    `json:"title"`
	Sort      int       `json:"sort"`
	ParentId  int       `json:"parent_id"`
	IsEnabled int       `json:"is_enabled"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
	Num       int       `json:"num"`
}
type ArticleCategoriesNum struct {
	Num int `json:"num"`
}

type SuggestArticleCategoryNameResult struct {
	Id           int    `json:"id"`
	Name         string `json:"name"`
	Account      string `json:"account"`
	Type         int    `json:"type"`
	Company      string `json:"company"`
	Industry     string `json:"industry"`
	Profession   string `json:"profession"`
	CategoryName string `json:"category_name"`
}

type SuggestArticleCategoryNameCountResult struct {
	CategoryName string `json:"category_name"`
	Num          int    `json:"num"`
}

type GetArticleCateSelectResult struct {
	Id        int    `json:"id"`
	Title     string `json:"title"`
	IsEnabled int    `json:"is_enabled"`
}

// 查找
func GetArticleCategories() ([]*ArticleCategoriesResult, error) {
	var data []*ArticleCategoriesResult
	err := DB.Table("article_categories").Order("sort asc").Find(&data).Error
	if err != nil {
		return nil, err
	}
	for i := 0; i < len(data); i++ {
		var num ArticleCategoriesNum
		DB.Table("articles").Select("count(*) as num").Where("article_category_id=?", data[i].ID).First(&num)
		data[i].Num = num.Num
	}
	return data, nil
}

func GetArticleCateSelect() ([]*GetArticleCateSelectResult, error) {
	var data []*GetArticleCateSelectResult
	err := DB.Table("article_categories").Where("is_enabled=?", 0).Order("sort asc").Find(&data).Error
	if err != nil {
		return nil, err
	}
	return data, nil
}

// 查找单个
func GetArticleCategoriesById(id int) (*ArticleCategories, error) {
	var data ArticleCategories
	err := DB.Table("article_categories").Where("id=?", id).Find(&data).Error
	if err != nil {
		return nil, err
	}
	return &data, nil
}

// 添加
func (a *ArticleCategories) AddArticleCategories() error {
	err := DB.Table("article_categories").Create(a).Error
	if err != nil {
		return err
	}
	return nil
}

// 编辑

func UpdateArticleCategories(data map[string]interface{}) error {
	err := DB.Table("article_categories").Where("id=?", data["id"]).Updates(data).Error
	if err != nil {
		return err
	}
	return nil
}

// 删除
func DeletedArticleCategories(id int) error {
	err := DB.Delete(ArticleCategories{}, "id=?", id).Error
	if err != nil {
		return err
	}
	return nil
}

// 获取建议分类名称

func GetSuggestArticleCategoryName() ([]*SuggestArticleCategoryNameResult, error) {
	var data []*SuggestArticleCategoryNameResult
	err := DB.Table("suggest_category_name as t1").Select("t1.category_name, t3.user_name as name , t2.phone as account, t3.company, t3.industry, t3.profession ").Joins("left join users as t2 on t1.user_id = t2.id").Joins("left join cards as t3 on t2.id = t3.user_id").Find(&data).Error
	if err != nil {
		return nil, err
	}
	return data, err
}

func GetSuggestArticleCategoryNameCount() ([]*SuggestArticleCategoryNameCountResult, error) {
	var data []*SuggestArticleCategoryNameCountResult
	err := DB.Table("suggest_category_name").Select("category_name, COUNT(*) as num").Group("category_name").Find(&data).Error
	if err != nil {
		return nil, err
	}
	return data, nil
}
